﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
namespace SZSLSystem.DAL
{
	/// <summary>
	///数据访问类 PurchaseAgreement (create by HC 2013/1/14 22:28:41)
	/// </summary>
	public class PurchaseAgreement
	{
		public PurchaseAgreement(){}
		public bool Exists(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select count(OID) from PurchaseAgreement");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			return DbHelperOleDb.Exists(strSql.ToString(), parameters);
		}

		public int Delete(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from PurchaseAgreement");
			strSql.Append(" where OID=@OID  ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
		}

		public int Add(SZSLSystem.Model.PurchaseAgreement model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("insert into PurchaseAgreement( ");
			strSql.Append("[OID],[AgreementCode],[BuyerOID],[RoomOID],[PriceType],[UnitPrice],[TotalPrice],[FinalPrice],[Payments],[PreferentialInfoOID],[SignTime],[SignAddress])");
			strSql.Append("values (@OID,@AgreementCode,@BuyerOID,@RoomOID,@PriceType,@UnitPrice,@TotalPrice,@FinalPrice,@Payments,@PreferentialInfoOID,@SignTime,@SignAddress)");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50),
					new SqlParameter ("@AgreementCode",SqlDbType.VarChar,50),
					new SqlParameter ("@BuyerOID",SqlDbType.VarChar,50),
					new SqlParameter ("@RoomOID",SqlDbType.VarChar,50),
					new SqlParameter ("@PriceType",SqlDbType.VarChar,50),
					new SqlParameter ("@UnitPrice",SqlDbType.VarChar,50),
					new SqlParameter ("@TotalPrice",SqlDbType.VarChar,50),
					new SqlParameter ("@FinalPrice",SqlDbType.VarChar,50),
					new SqlParameter ("@Payments",SqlDbType.VarChar,50),
					new SqlParameter ("@PreferentialInfoOID",SqlDbType.VarChar,50),
					new SqlParameter ("@SignTime",SqlDbType.DateTime),
					new SqlParameter ("@SignAddress",SqlDbType.VarChar,50)};
			parameters[0].Value = model.OID;
			parameters[1].Value = model.AgreementCode;
			parameters[2].Value = model.BuyerOID;
			parameters[3].Value = model.RoomOID;
			parameters[4].Value = model.PriceType;
			parameters[5].Value = model.UnitPrice;
			parameters[6].Value = model.TotalPrice;
			parameters[7].Value = model.FinalPrice;
			parameters[8].Value = model.Payments;
			parameters[9].Value = model.PreferentialInfoOID;
			parameters[10].Value = model.SignTime;
			parameters[11].Value = model.SignAddress;
			object obj = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
			if (obj == null)
			{
				return 1;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		public  SZSLSystem.Model.PurchaseAgreement GetModel(string OID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select top 1 [OID],[AgreementCode],[BuyerOID],[RoomOID],[PriceType],[UnitPrice],[TotalPrice],[FinalPrice],[Payments],[PreferentialInfoOID],[SignTime],[SignAddress] from PurchaseAgreement");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50)};
			parameters[0].Value = OID;

			SZSLSystem.Model.PurchaseAgreement model=new SZSLSystem.Model.PurchaseAgreement();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				DataRow row = ds.Tables[0].Rows[0];
						model.OID=row["OID"].ToString();
						model.AgreementCode=row["AgreementCode"].ToString();
						model.BuyerOID=row["BuyerOID"].ToString();
						model.RoomOID=row["RoomOID"].ToString();
						model.PriceType=row["PriceType"].ToString();
						model.UnitPrice=row["UnitPrice"].ToString();
						model.TotalPrice=row["TotalPrice"].ToString();
						model.FinalPrice=row["FinalPrice"].ToString();
						model.Payments=row["Payments"].ToString();
						model.PreferentialInfoOID=row["PreferentialInfoOID"].ToString();
					if (row["SignTime"].ToString()!="")
					{
						model.SignTime=DateTime.Parse(row["SignTime"].ToString());
					}
						model.SignAddress=row["SignAddress"].ToString();

				return model;
			}
			else
			{
				return null;
			}
		}

		public int Update(SZSLSystem.Model.PurchaseAgreement model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("update PurchaseAgreement set ");
			strSql.Append("[OID]=@OID,");
			strSql.Append("[AgreementCode]=@AgreementCode,");
			strSql.Append("[BuyerOID]=@BuyerOID,");
			strSql.Append("[RoomOID]=@RoomOID,");
			strSql.Append("[PriceType]=@PriceType,");
			strSql.Append("[UnitPrice]=@UnitPrice,");
			strSql.Append("[TotalPrice]=@TotalPrice,");
			strSql.Append("[FinalPrice]=@FinalPrice,");
			strSql.Append("[Payments]=@Payments,");
			strSql.Append("[PreferentialInfoOID]=@PreferentialInfoOID,");
			strSql.Append("[SignTime]=@SignTime,");
			strSql.Append("[SignAddress]=@SignAddress");
			strSql.Append(" where OID=@OID ");
			SqlParameter[] parameters = {
					new SqlParameter ("@OID",SqlDbType.VarChar,50),
					new SqlParameter ("@AgreementCode",SqlDbType.VarChar,50),
					new SqlParameter ("@BuyerOID",SqlDbType.VarChar,50),
					new SqlParameter ("@RoomOID",SqlDbType.VarChar,50),
					new SqlParameter ("@PriceType",SqlDbType.VarChar,50),
					new SqlParameter ("@UnitPrice",SqlDbType.VarChar,50),
					new SqlParameter ("@TotalPrice",SqlDbType.VarChar,50),
					new SqlParameter ("@FinalPrice",SqlDbType.VarChar,50),
					new SqlParameter ("@Payments",SqlDbType.VarChar,50),
					new SqlParameter ("@PreferentialInfoOID",SqlDbType.VarChar,50),
					new SqlParameter ("@SignTime",SqlDbType.DateTime),
					new SqlParameter ("@SignAddress",SqlDbType.VarChar,50)};
			parameters[0].Value = model.OID;
			parameters[1].Value = model.AgreementCode;
			parameters[2].Value = model.BuyerOID;
			parameters[3].Value = model.RoomOID;
			parameters[4].Value = model.PriceType;
			parameters[5].Value = model.UnitPrice;
			parameters[6].Value = model.TotalPrice;
			parameters[7].Value = model.FinalPrice;
			parameters[8].Value = model.Payments;
			parameters[9].Value = model.PreferentialInfoOID;
			parameters[10].Value = model.SignTime;
			parameters[11].Value = model.SignAddress;

			return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
		}

		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select [OID],[AgreementCode],[BuyerOID],[RoomOID],[PriceType],[UnitPrice],[TotalPrice],[FinalPrice],[Payments],[PreferentialInfoOID],[SignTime],[SignAddress]");
			strSql.Append(" FROM PurchaseAgreement ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public DataSet GetList(int top,string strWhere,string fieldOrder)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("select");
			if(top>0)
			{
				strSql.Append(" top "+top.ToString());
			}
			strSql.Append(" [OID],[AgreementCode],[BuyerOID],[RoomOID],[PriceType],[UnitPrice],[TotalPrice],[FinalPrice],[Payments],[PreferentialInfoOID],[SignTime],[SignAddress]");
			strSql.Append(" FROM PurchaseAgreement ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
				strSql.Append(" order by "+fieldOrder);
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public DataSet GetList(string showFields,string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
				strSql.Append(" select "+showFields);
			strSql.Append(" FROM PurchaseAgreement ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		public object GetSingle(string fieldName,string strWhere)
		{
			StringBuilder strSql = new StringBuilder();
				strSql.Append(" select "+fieldName);
			strSql.Append(" FROM PurchaseAgreement ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.GetSingle(strSql.ToString());
		}

	}
}